package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;

    @RequestMapping("/findPermission")
    public Result findPermission(){
        try {
            List<Permission> permissions = permissionService.findPermission();
            System.out.println("permissions = " + permissions);
            return new Result(true,"查询权限成功",permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限失败");
        }
    }


    /**
     *
     * @param permission 权限
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true, MessageConst.ADD_Permission_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConst.ADD_Permission_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return 分页结果 总记录数和当前页结果
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findPage(queryPageBean);
    }

    /**
     * 根据 id 删除 权限  判断是否关联
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delById(id);
            return  new Result(true,MessageConst.DELETE_Permission_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return  new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConst.DELETE_Permission_FAIL );
        }
    }

    /**
     * 编辑 回显
     * @param id
     * @return  权限对象
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return  new Result(true,MessageConst.QUERY_Permission_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConst.QUERY_Permission_FAIL);
        }
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,MessageConst.EDIT_Permission_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.EDIT_Permission_FAIL);
        }

    }
}
